<?php if(!defined('SPRITCH')) { exit(); }

// Spritch - An opensource PHP5 web framework

// Author: Luca Degasperi
// Licence: GNU GPL
// Site: http://www.spritch.org/

// Class: Validator Helper
// Summary: This class provides validation functions
// Since: 0.9.1
 
class validator_helper extends helper
{
	public function __construct()
	{
		
	}
	
	public function email($email,$callback = null)
	{
		return (bool)filter_var($email,FILTER_VALIDATE_EMAIL);
	}
	
	public function url($url)
	{
		return (bool)filter_var($url,FILTER_VALIDATE_URL,FILTER_FLAG_HOST_REQUIRED);
	}
	
	public function ip($ip,$v6 = false)
	{
		if($v6 == true)
			return (bool)filter_var($ip,FILTER_VALIDATE_IP,FILTER_FLAG_NO_PRIV_RANGE|FILTER_FLAG_NO_RES_RANGE);
		return (bool)filter_var($ip, FILTER_VALIDATE_IP,FILTER_FLAG_NO_PRIV_RANGE|FILTER_FLAG_NO_RES_RANGE|FILTER_FLAG_IPV4);
	}
	
	public function alpha($text)
	{
		return ctype_alpha($text);
	}
	
	public function alpha_numeric($text)
	{
		return ctype_alnum($text);
	}
	
	public function alpha_dash($text)
	{
		return (bool)preg_match('/^[-a-z0-9_]++$/iD',$text);
	}
	
	public function digits($number)
	{
		return ctype_digit($number);
	}
	
	public static function numeric($number)
	{
		if(is_numeric($number) and preg_match('/^[-0-9.]++$/D',$number))
			return true;
		return false;
	}
	
	public function plain_text($text)
	{
		return (bool)preg_match('/^[-\pL\pN\pZ_.]++$/uD',$text);
	}
} 
?>